﻿using BookStores.IDomain;
using Dapper.Database;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Utility.Extensions;

namespace BookStores.Domain
{
    /// <summary>
    /// 
    /// </summary>
    public class GoodsTag : IGoodsTag
    {
        BookStores.Contracts.IGoodsTagRepository m_IGoodsTagRepository;
        public GoodsTag(BookStores.Contracts.IGoodsTagRepository goodsTagRepository)
        {
            m_IGoodsTagRepository = goodsTagRepository;
        }

        /// <summary>
        /// 获取标签列表
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public async Task<IEnumerable<Entity.GoodsTag>> GetGoodsTag(string keyWord,string tagType, int pageIndex = 1, int pageSize = 10)
        {
            keyWord = keyWord.StrTrim();
            string[] kws = keyWord?.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            return await m_IGoodsTagRepository.Queryable().WhereIF(null != kws && kws.Length > 1, t => kws.Contains(t.ID))
                                                          .WhereIF((null == kws || kws.Length == 1) && !string.IsNullOrEmpty(keyWord), t =>
                                                                           t.Tag.Contains(keyWord ?? "")
                                                          )
                                                          .Where(t => string.Equals(t.Tag_Type, tagType, StringComparison.OrdinalIgnoreCase))
                                                          .ToPageListAsync(pageIndex, pageSize);
        }
    }
}